package com.wpen.unit.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DateUtil {
	public static final String FORMAT_DATE = "yyyy-MM-dd";
	public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
	public static final String FORMAT_TIME = "HH:mm:ss";
	public static final String FORMAT_DATE2 = "yyyyMMdd";
	public static final String FORMAT_DATETIME2 = "yyyyMMddHHmmss";
	public static final String FORMAT_TIME2 = "HHmmss";
	public static final String FORMAT_DATETIME3 = "yyyyMMddHHmmssSSS";

	/**
	 * 执行日期，int类型
	 * 
	 * @return
	 */
	public static String getAcctDate() {
		return format(FORMAT_DATE2);
	}

	/**
	 * 获得系统当前日期
	 *
	 * @return 格式为 yyyy-MM-dd
	 * @throws Exception
	 */
	public static String getDate() throws RuntimeException {
		return format(null, null);
	}

	/**
	 * 转换给定日期为字符串形式
	 * 
	 * @param date
	 * @return yyyy-MM-dd
	 * @throws RuntimeException
	 */
	public static String getDate(Date date) throws RuntimeException {
		return format(date, null);
	}

	/**
	 * 将日期类型转成成字符串
	 * 
	 * @param date
	 * @param format
	 * @return
	 * @throws RuntimeException
	 */
	public static String format(Date date, String format) {
		if ((format == null) || ("".equals(format)))
			format = FORMAT_DATE;
		if (date == null)
			date = new Date();
		DateFormat dateformat = new SimpleDateFormat(format);
		return dateformat.format(date);
	}

	public static String format() {
		return format(null, null);
	}

	public static String format(String format) {
		return format(null, format);
	}

	/**
	 * 将一个字符串日期转换成date类型
	 * 
	 * @param dateStr
	 * @param format  给定字符串日期的格式
	 * @return
	 */
	public static Date parse(String dateStr, String format) {
		if ((format == null) || ("".equals(format)))
			format = FORMAT_DATE;
		DateFormat dateformat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = dateformat.parse(dateStr);
		} catch (ParseException e) {
			log.error(
					"DateUtil.parse() Error : !!!!!!!!input \"" + dateStr + "\" and format " + format + " is worry! ");
		}
		return date;
	}

	/**
	 * 将一个字符串日期转换成date类型
	 * 
	 * @param dateStr
	 * @return
	 * @throws RuntimeException
	 */
	public static Date parse(String dateStr) throws RuntimeException {
		if (dateStr == null)
			return null;
		String value = dateStr.replaceAll("：", ":");
		if ((value != null) && (value.length() == 28) && (value.indexOf("CST") == 20)) {
			String year = value.substring(24, 28);
			String date = value.substring(8, 10);
			String temp = value.substring(4, 7);
			if (temp.equals("Jan"))
				value = year + "-" + "01" + "-" + date;
			if (temp.equals("Feb"))
				value = year + "-" + "02" + "-" + date;
			if (temp.equals("Mar"))
				value = year + "-" + "03" + "-" + date;
			if (temp.equals("Apr"))
				value = year + "-" + "04" + "-" + date;
			if (temp.equals("May"))
				value = year + "-" + "05" + "-" + date;
			if (temp.equals("Jun"))
				value = year + "-" + "06" + "-" + date;
			if (temp.equals("Jul"))
				value = year + "-" + "07" + "-" + date;
			if (temp.equals("Aug"))
				value = year + "-" + "08" + "-" + date;
			if (temp.equals("Sep"))
				value = year + "-" + "09" + "-" + date;
			if (temp.equals("Oct"))
				value = year + "-" + "10" + "-" + date;
			if (temp.equals("Nov"))
				value = year + "-" + "11" + "-" + date;
			if (temp.equals("Dec"))
				value = year + "-" + "12" + "-" + date;
		}
		if ((value.indexOf("年") > 0) && (value.indexOf("月") > 0) && (value.indexOf("日") > 0) && (value.indexOf("时") > 0)
				&& (value.indexOf("分") > 0) && (value.indexOf("秒") > 0) && (value.indexOf("毫秒") > 0))
			return parse(value, "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒");
		if ((value.indexOf("年") > 0) && (value.indexOf("月") > 0) && (value.indexOf("日") > 0) && (value.indexOf("时") > 0)
				&& (value.indexOf("分") > 0) && (value.indexOf("秒") > 0))
			return parse(value, "yyyy年MM月dd日 HH时mm分ss秒");
		if ((value.indexOf("/") > 0) && (value.trim().length() == 10))
			return parse(value, "yyyy/MM/dd");
		if ((value.indexOf("-") > 0) && (value.trim().length() == 19))
			return parse(value, "yyyy-MM-dd HH:mm:ss");
		if ((value.indexOf("-") > 0) && (value.trim().length() == 23))
			return parse(value, "yyyy-MM-dd HH:mm:ss.SSS");
		if ((value.indexOf("-") > 0) && (value.trim().length() == 10))
			return parse(value, FORMAT_DATE);
		if ((value.indexOf(":") > 0) && (value.trim().length() == 8))
			return parse(value, "HH:mm:ss");
		if ((value.indexOf(":") > 0) && (value.trim().length() == 12))
			return parse(value, "HH:mm:ss.SSS");
		if (value.trim().length() == 14)
			return parse(value, "yyyyMMddHHmmss");
		if (value.trim().length() == 17)
			return parse(value, "yyyyMMddHHmmssSSS");
		if ((value.indexOf(" ") > 0) && (value.trim().length() == 15))
			return parse(value, "yyyyMMdd HHmmss");
		if ((value.indexOf(" ") > 0) && (value.trim().length() == 18))
			return parse(value, "yyyyMMdd HHmmssSSS");
		if (value.trim().length() == 8)
			return parse(value, "yyyyMMdd");
		if (value.trim().length() == 6)
			return parse(value, "HHmmss");
		if (value.trim().length() == 9)
			return parse(value, "HHmmssSSS");
		return null;
	}

	public static double daysBetween(Date startdate, Date enddate) throws ParseException {
		long time2 = enddate.getTime();
		long time1 = startdate.getTime();
		long diff = time2 - time1;
		return Double.parseDouble(diff / 86400000L + "." + diff % 86400000L / 864000L);
	}

	public static double daysBetween(String startdate, String enddate, String dateFormat) throws ParseException {
		return daysBetween(parse(startdate, dateFormat), parse(enddate, dateFormat));
	}

	/**
	 * 得到指定日期前/后offset天的日期
	 *
	 * @param dateString 以"yyyy-MM-dd"格式指定的日期
	 * @param offset     偏移量
	 * @return String
	 * @throws RuntimeException
	 */
	public static String offset(String dateStr, int dayOfYear) throws RuntimeException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parse(dateStr, null));
		calendar.add(Calendar.DAY_OF_YEAR, dayOfYear);
		return format(calendar.getTime(), null);
	}

	public static String offset(int dayOfYear) throws RuntimeException {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_YEAR, dayOfYear);
		return format(calendar.getTime(), null);
	}

	public static String offset(int field, int amount, String format) throws RuntimeException {
		Calendar calendar = Calendar.getInstance();
		calendar.add(field, amount);
		return format(calendar.getTime(), format);
	}

	public static String offset(Date date, int field, int amount, String format) throws RuntimeException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(field, amount);
		return format(calendar.getTime(), format);
	}

	public static String offset(String dateStr, String dateFormat, int field, int amount, String format)
			throws RuntimeException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parse(dateStr, dateFormat));
		calendar.add(field, amount);
		return format(calendar.getTime(), format);
	}

	public static long compareTime(String sStartdates, String bEnddates, String dateFormat) throws ParseException {
		Date sStartdate = parse(sStartdates, dateFormat);
		Date bEnddate = parse(bEnddates, dateFormat);
		if ((dateFormat == null) || ("".equals(dateFormat)))
			dateFormat = FORMAT_DATE;
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		sStartdate = sdf.parse(sdf.format(sStartdate));
		bEnddate = sdf.parse(sdf.format(bEnddate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(sStartdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bEnddate);
		long time2 = cal.getTimeInMillis();
		Long between_days = Long.valueOf((time2 - time1) / 1000L);
		return between_days.longValue();
	}

	public static String execTime(Date start, Date end) {
		if (start == null || end == null) {
			return "--";
		}
		long startLong = start.getTime();
		long endLong = end.getTime();
		long second = endLong - startLong;
		second = second /1000 + 1;
		if (second > 60) {
			long min = second / 60;
			if (min > 60) {
				long hour = min / 60;
				return hour + "时" + min % 60 + "分" + second % 60 + "秒";
			} else {
				return min % 60 + "分" + second % 60 + "秒";
			}
		} else {
			return "" + (second % 60) + "秒";
		}
	}

	public static void main(String[] args) throws Exception {
		String _sys_time = "2016-9-5 18:07:10";
		String _now_time = format(null, "yyyy-MM-dd hh:mm:ss");
		try {
			long number = compareTime(_sys_time, _now_time, "yyyy-MM-dd hh:mm:ss");
			System.out.println(number);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}